package com.itheima.sms.redismq;

import com.itheima.sms.factory.SmsFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

/**
 * Redis队列-----消费者
 * 监听消息队列：TOPIC_HIGH_SMS，高优先级的短信，如验证码之类的短信
 */
@Component
@Slf4j
public class HighSmsListener extends Thread {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SmsFactory smsFactory;

    private String queueKey = "TOPIC_HIGH_SMS";

    @Value("${spring.redis.queue.pop.timeout}")
    private Long popTimeout = 8000L;

    private ListOperations listOps;

    @PostConstruct
    private void init() {
        listOps = redisTemplate.opsForList();
        this.start();
    }

    @Override
    public void run() {
        log.info("监听队列: {}中的短信消息", queueKey);

        // 使用死循环, 做到持续监听, 并将这个命令设置堵塞时间8S
        while (true) {
            String message = (String) listOps.rightPop(queueKey, popTimeout, TimeUnit.MILLISECONDS);
            if (StringUtils.isNotBlank(message)) {
                // 队列中有消息, 需要发送短信
                log.info("队列: {}中收到短信发送消息: {}", queueKey, message);
                // 调用短信发送工厂发送短信
                smsFactory.send(message);
            }
        }
        // while (true) {
        //     log.debug("{} 监听中", queueKey);
        //     System.out.println("监听中..." + new Date());
        //     String message = (String) listOps.rightPop(queueKey, popTimeout, TimeUnit.MILLISECONDS);
        //     if (null != message) {
        //         log.info("{} 收到消息:{}", queueKey, message);
        //         smsFactory.send(message);
        //     }
        // }
    }
}
